package scrapper;

import java.io.IOException;
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.HttpStatusException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public abstract class AbstractScrapper {

	private String url;
	private String agent;
	private Document page;
	private Map<String, String> loginCookies;

	private String title;
	private String file;
	private String author;
	private String authorID;
	private String date;
	private String desc;
	private String[] tags;
	private boolean sfw;
	private int clics;
	private int favs;

	public AbstractScrapper(String id, String agent) {
		this.url = buildURL(id);
		this.agent = agent;
	}

	public int connect() throws IOException {
		try {
			Connection con = Jsoup.connect(url);
			con.timeout(0);
			con.userAgent(agent);
			this.page = con.get();
			//this.page = Jsoup.connect(url).userAgent(agent).get();
		}
		catch (HttpStatusException e) {
			System.err.println(e.getStatusCode());
			return e.getStatusCode();
		}
		return 0;
	}
	public int connectWithCookies() throws IOException {
		try {
			Connection con = Jsoup.connect(url);
			con.timeout(0);
			con.userAgent(agent);
			con.cookies(getLoginCookies());
			this.page = con.get();
			//this.page = Jsoup.connect(url).userAgent(agent).cookies(getLoginCookies()).get();
		}
		catch (HttpStatusException e) {
			System.err.println(e.getStatusCode());
			return e.getStatusCode();
		}
		return 0;
	}

	public abstract void login(String username, String password) throws IOException;
	public abstract String buildURL(String id);
	public abstract boolean buildPage() throws HttpStatusException;

	public String getUrl() { return url; }
	public void setUrl(String url) { this.url = url; }

	public String getAgent() { return agent; }
	public void setAgent(String agent) { this.agent = agent; }

	public Document getPage() { return page; }
	public void setPage(Document page) { this.page = page; }

	public Map<String, String> getLoginCookies() { return loginCookies; }
	public void setLoginCookies(Map<String, String> loginCookies) { this.loginCookies = loginCookies; }

	public String getTitle() { return title; }
	public void setTitle(String title) { this.title = title; }

	public String getFile() { return file; }
	public void setFile(String file) { this.file = file; }

	public String getAuthor() { return author; }
	public void setAuthor(String author) { this.author = author; }

	public String getAuthorID() { return authorID; }
	public void setAuthorID(String authorID) { this.authorID = authorID; }

	public String getDate() { return date; }
	public void setDate(String date) { this.date = date; }

	public String getDesc() { return desc; }
	public void setDesc(String desc) { this.desc = desc; }

	public String[] getTags() { return tags; }
	public void setTags(String[] tags) { this.tags = tags; }

	public boolean isSfw() { return sfw; }
	public void setSfw(boolean sfw) { this.sfw = sfw; }

	public int getClics() { return clics; }
	public void setClics(int clics) { this.clics = clics; }

	public int getFavs() { return favs; }
	public void setFavs(int favs) { this.favs = favs; }

	@Override
	public String toString() { return getTitle()+" par "+getAuthor()+" à "+getDate(); }

}
